package com.yanqu.road.server.manger.system;

import com.yanqu.road.dao.impl.servercenter.MiniGameConfigDaoImpl;
import com.yanqu.road.dao.impl.servercenter.MiniGameSortConfigDaoImpl;
import com.yanqu.road.entity.config.minigame.MiniGameConfig;
import com.yanqu.road.pb.server.LoadProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;

import java.util.*;

public class MiniGameConfigMgr extends TempMgr {

    private static List<MiniGameConfig> openMiniGameList = new ArrayList<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        long serverId = GameServer.getInstance().getServerId();
        int channelId = ServerListMgr.getChannelId(serverId);
        Map<Integer, MiniGameConfig> miniGameConfigMap = new MiniGameSortConfigDaoImpl().getMiniGameConfigMap();
        Set<Integer> openMiniGame = new MiniGameConfigDaoImpl().getOpenMiniGame(channelId);

        List<MiniGameConfig> dataList = new ArrayList<>();
        for (Integer gameId : openMiniGame) {
            if (miniGameConfigMap.containsKey(gameId)) {
                dataList.add(miniGameConfigMap.get(gameId));
            }
        }
        dataList.sort(Comparator.comparingInt(MiniGameConfig::getSort));
        openMiniGameList = dataList;
        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            syncConfig(player);
        }
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void syncConfig(GamePlayer player) {
        LoadProto.SyncOpenMiniGameIdListMsg.Builder syncMsg = parseSyncOpenMiniGameIdListMsg(openMiniGameList);
        player.sendPacket(ClientProtocol.U_SYNC_OPEN_MINI_GAME_ID_LIST, syncMsg);
    }

    public static boolean hasMiniGame() {
        return openMiniGameList.size() > 0;
    }

    public static boolean hasMiniGame(int gameId) {
        if(null == openMiniGameList){
            return false;
        }
        for(MiniGameConfig data : openMiniGameList){
            if(data.getGameId() == gameId){
                return true;
            }
        }
        return false;
    }

    public static LoadProto.MiniGameConfigTempMsg.Builder parseMiniGameConfigTempMsg(MiniGameConfig data){
        if(null == data){
            return null;
        }
        LoadProto.MiniGameConfigTempMsg.Builder msg = LoadProto.MiniGameConfigTempMsg.newBuilder();
        msg.setGameId(data.getGameId());
        msg.setAdId(data.getAdId());
        return msg;
    }

    public static LoadProto.SyncOpenMiniGameIdListMsg.Builder parseSyncOpenMiniGameIdListMsg(List<MiniGameConfig> dataList){
        LoadProto.SyncOpenMiniGameIdListMsg.Builder listMsg = LoadProto.SyncOpenMiniGameIdListMsg.newBuilder();
        for(MiniGameConfig data : dataList){
            LoadProto.MiniGameConfigTempMsg.Builder msg = parseMiniGameConfigTempMsg(data);
            if(null != msg){
                listMsg.addDataList(msg);
            }
            listMsg.addGameIdList(data.getGameId());
        }
        return listMsg;
    }

    public static MiniGameConfig getMiniGameConfig(int adId){
        if(null == openMiniGameList){
            return null;
        }
        for(MiniGameConfig data : openMiniGameList){
            if(data.getAdId() == adId){
                return data;
            }
        }
        return null;
    }
}
